#include <stdio.h>
#include <aos/kernel.h>
#include <stdbool.h>
#include "isp_irq_cb.h"
#include "io.h"
#include "wrapper.h"
#include "if_v.h"
#include "isp_function.h"
#include "drv_delay.h"

extern int g_rgbSensorCapMode;
extern int g_irSensorConvergent[];

extern volatile int g_rgb_img_ready;

extern volatile uint32_t ispProcFlag[ISP_NUM];
extern volatile int isp_mi_event[ISP_NUM];
extern volatile int isp_mode[ISP_NUM];
extern volatile uint32_t curVal[ISP_NUM];
extern int g_mi_debug;  //1/2/4/8[]
extern int if_vNoifyIrSpkData(int isp_id);

int volatile ispx_buf_index = 0;

#define MI_DEBUG(idx,status)    \
        do{ \
            if((g_mi_debug == 0x8) || ( (g_mi_debug) && ((g_mi_debug>>idx) & idx) == idx ) )   \
                printf("%s isp-%d, status 0x%x Convergent %d\n",__func__,idx,status,g_irSensorConvergent[idx]);    \
        }while(0);

void isp0_event_cb_fun(int32_t idx, isp_event_e event,uint32_t status)
{
#if 0
	static int count = 0;
	if(count%30 == 0)
	    printf("isp 0 %d \n",count++);
#endif

    if (event == ISP_EVENT_RECEIVED_ERROR)
    {
    	printf("isp0 rece error\n");
    	isp_reg_write(0,MARVIN_CTRL_IRCL, 0x15);
    	mdelay(5);
    	isp_reg_write(0,MARVIN_CTRL_IRCL, 0x55);
    	mdelay(15);
    	isp_reg_write(0,MARVIN_CTRL_IRCL, 0x0);
        isp_start(0);
    	//printf("isp data error\n");
    }
    else
    {
#if 0
        HalIsrHandlerSrc(0, status);
#else
		curVal[idx] |= status;
		ispProcFlag[idx] = 1;

#endif
    }
}

void isp1_event_cb_fun(int32_t idx, isp_event_e event,uint32_t status)
{
    //curVal[idx] |= status;
    //ispProcFlag[idx] = 1;

    if (event == ISP_EVENT_RECEIVED_ERROR)
    {
        printf("isp1 rece error\n");
        isp_reg_write(1,MARVIN_CTRL_IRCL, 0x15);
        mdelay(5);
        isp_reg_write(1,MARVIN_CTRL_IRCL, 0x55);
        mdelay(15);
        isp_reg_write(1,MARVIN_CTRL_IRCL, 0x0);
        isp_start(1);
    }
    else
    {
        curVal[idx] |= status;
        ispProcFlag[idx] = 1;
        
        //printf("%s  \n",__func__);
    }
}


void isp2_event_cb_fun(int32_t idx, isp_event_e event,uint32_t status)
{
    // static uint32_t count = 0;
    //printf("isp1_cb %d, mis %08x\n", ++count, status);

    if (event == ISP_EVENT_RECEIVED_ERROR)
    {
        printf("isp2 rece error\n");
        isp_reg_write(2,MARVIN_CTRL_IRCL, 0x15);
        mdelay(5);
        isp_reg_write(2,MARVIN_CTRL_IRCL, 0x55);
        mdelay(15);
        isp_reg_write(2,MARVIN_CTRL_IRCL, 0x0);
        isp_start(1);
    }
    else
    {
        curVal[idx] |= status;
        ispProcFlag[idx] = 1;
        
        //printf("%s  \n",__func__);
    }
}

extern if_v_sema_t ifv_mi_ir_rgb_semp;


void isp_mi0_event_cb_fun(int32_t idx, isp_mi_event_e event,uint32_t status)
{
    MI_DEBUG(idx,status);

    //if(status & 0x7E){ //bit 1~6, 
    if(status & 0x2){   
#if defined(IR_SENSOR_ISP_BYPASS)
        isp_stop(idx);
        //printf("========= %s ISP0 data 0x%x=========\r\n", __func__,status);
        if_vNotifyIspData(1);
#else		
            if(g_irSensorConvergent[idx] == 1 ){
                //ov9282_mode_config(0);
                if_vNoifyIrSpkData(idx);
            }
#endif 		
    }

    if(status & 0x04)
    {
        if (!g_rgb_img_ready)
        {
            isp_mi_event[idx] = 1;
        }
    }
    if (event == ISP_MI_EVENT_RECEIVED_ERROR)
    {
        printf("isp mi0 receive data error\n");
    }
    else if(event == ISP_MI_EVENT_FRAME_END)
    {
        //printf("isp mi frame end \n");
    }
}


extern uint8_t isp1_mi_buf0_ready_flag;
extern uint8_t isp1_mi_buf1_ready_flag;
extern uint8_t isp1_mi_buf2_ready_flag;
extern uint8_t isp1_buf_change_flag;
extern int g_vis_buffer_num;
// static int isp_mi1_buf_config = 0;
extern uint8_t *g_tx_vis_buff;
extern bool g_tx_enable_flag;
extern uint8_t *g_vis_buffer[];

extern int irreco_enable_flag;
extern int g_reco_result;
void isp_mi1_buf_reconfig(int num, int set_num);

extern uint8_t *g_vis_buffer[];
extern int g_vis_buffer_num;
#if 0
void isp_mi1_buf_reconfig(int num, int set_num)
{
    if (!g_vis_buffer_num)
        return;
	//printf("g_vis_buffer[%d] = %p \n", set_num, g_vis_buffer[set_num]);
	if(num == 0)
		isp_reg_write(ISP_1, MI_SP_Y_BASE_AD,  g_vis_buffer[set_num]);
	else if(num ==1)
		isp_reg_write(ISP_1, MI_SP_Y_BASE_AD2, g_vis_buffer[set_num]);
	else if(num ==2)
		isp_reg_write(ISP_1, MI_SP_Y_BASE_AD3, g_vis_buffer[set_num]);
}

static void isp_mi1_buf_with_mi2_config(int next_num, int num)
{
	if(!g_vis_buffer_num)
		return ;
	
	if(!isp_mi1_buf_config && g_tx_enable_flag && irreco_enable_flag)
	{
		//isp_mi1_buf_reconfig(next_num, 2);
		isp_mi1_buf_config = next_num + 1;
		irreco_enable_flag = 0;
		//printf("vis_buffer enable %d!!!\n", next_num);
	}
	else if((isp_mi1_buf_config == (num+1)) && g_tx_enable_flag)
	{
		g_tx_vis_buff = g_vis_buffer[2];
		g_tx_enable_flag = false;
		//isp_mi1_buf_reconfig(num, 0);
		isp_mi1_buf_config = 0;
		g_reco_result = USERCMDRECOGNIZERESULT_NONE;
		//printf("vis_buffer ready %d!!!\n", num);
	}
	
}
#endif



void isp_mi1_event_cb_fun(int32_t idx, isp_mi_event_e event,uint32_t status)
{
    // uint32_t isp_temp;
    MI_DEBUG(idx,status);

    if(status & 0x7E){ //bit 1~6, 
        //mipi2isp_channel_stop(MIPI_2,ISP_1);
        //isp_stop(idx);

        //printf("========= %s ISP1 data 0x%x=========\r\n", __func__, status);


        if(status & 0x02)
        { 
        	isp1_mi_buf0_ready_flag=1;
          	isp1_buf_change_flag=1;
            ispx_buf_index = 0;
			//isp_mi1_buf_reconfig(2, 0);
        }
        if(status & 0x04)
        {
			isp1_mi_buf1_ready_flag=1;
			isp1_buf_change_flag=1;
            ispx_buf_index = 1;
			//isp_mi1_buf_reconfig(0, 1);
            //printf("buf2 \n");
        }
        if(status & 0x08)
        { 
        	isp1_mi_buf2_ready_flag=1;
			isp1_buf_change_flag=1;
            ispx_buf_index = 2;
			//isp_mi1_buf_reconfig(1, 2);
        }
    
        if(g_rgbSensorCapMode){
            printf("g_rgbSensorCapMode =%d \n", g_rgbSensorCapMode);
            if_vNotifyIspData(0);
            g_rgbSensorCapMode=0;
        }
    }


    if(status & 0x02)
    {
        if(isp_mode[idx] == SNAP_MODE)
        {
            //mipi2isp_channel_stop(MIPI_1,ISP_1);
            //isp_stop(idx);
        }
        isp_mi_event[idx] = 1;
    }
    
    if (event == ISP_MI_EVENT_RECEIVED_ERROR)
    {
        printf("isp mi1 receive data error\n");
    }
    else if(event == ISP_MI_EVENT_FRAME_END)
    {
        //printf("isp mi frame end \n");
    }
}


void isp_mi2_event_cb_fun(int32_t idx, isp_mi_event_e event,uint32_t status)
{
    MI_DEBUG(idx,status);

    //if(status & 0x7E){ //bit 1~6, 
    if(status & 0x2){   
#if defined(IR_SENSOR_ISP_BYPASS)
        isp_stop(idx);
        //printf("========= %s ISP0 data 0x%x=========\r\n", __func__,status);
        if_vNotifyIspData(1);
#else		
            if(g_irSensorConvergent[idx] == 1 ){
                //ov9282_mode_config(0);
                if_vNoifyIrSpkData(1);
            }
#endif 		
    }

    if(status & 0x04)
    {
        if (!g_rgb_img_ready)
        {
            isp_mi_event[idx] = 1;
        }
    }
    if (event == ISP_MI_EVENT_RECEIVED_ERROR)
    {
        printf("isp mi2 receive data error\n");
    }
    else if(event == ISP_MI_EVENT_FRAME_END)
    {
        //printf("isp mi frame end \n");
    }
}


